﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Serialization.Formatters;
using System.Collections;
using System.Runtime.Remoting.Channels.Tcp;

namespace ZPod.MTG.Communication.Remoting
{
	/// <summary>
	/// Servidor basado en Remoting
	/// </summary>
	public class Server : BaseServer
	{
		/// <summary>
		/// Inicia un servidor basado en remoting.
		/// Se utiliza la direccion tcp://localhost:port/MTGGameServer
		/// </summary>
		/// <param name="port"></param>
		public static void CreateGameServer(int port, string serverName)
		{
			//RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off;

			// Creating a custom formatter for a TcpChannel sink chain.
			var provider = new BinaryServerFormatterSinkProvider();
			
			provider.TypeFilterLevel = TypeFilterLevel.Full;

			// Creating the IDictionary to set the port on the channel instance.
			IDictionary properties = new Hashtable();
			properties["port"] = port;

			TcpChannel channel = new TcpChannel(properties, null, provider);
			ChannelServices.RegisterChannel(channel, false);

			RemotingConfiguration.RegisterWellKnownServiceType(
				typeof(Server),
				serverName,
				WellKnownObjectMode.Singleton);
		}
	}
}